package com.castle.fortress.admin.study.course.service.impl;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.study.course.entity.StudyCourseClassifyEntity;
import com.castle.fortress.admin.study.course.dto.StudyCourseClassifyDto;
import com.castle.fortress.admin.study.course.mapper.StudyCourseClassifyMapper;
import com.castle.fortress.admin.study.course.service.StudyCourseClassifyService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 课程/课件分类表 服务实现类
 *
 * @author castle
 * @since 2023-01-30
 */
@Service
public class StudyCourseClassifyServiceImpl extends ServiceImpl<StudyCourseClassifyMapper, StudyCourseClassifyEntity> implements StudyCourseClassifyService {
    /**
    * 获取查询条件
    * @param studyCourseClassifyDto
    * @return
    */
    public QueryWrapper<StudyCourseClassifyEntity> getWrapper(StudyCourseClassifyDto studyCourseClassifyDto){
        QueryWrapper<StudyCourseClassifyEntity> wrapper= new QueryWrapper();
        if(studyCourseClassifyDto != null){
            StudyCourseClassifyEntity studyCourseClassifyEntity = ConvertUtil.transformObj(studyCourseClassifyDto,StudyCourseClassifyEntity.class);
            wrapper.like(StrUtil.isNotEmpty(studyCourseClassifyEntity.getName()),"name",studyCourseClassifyEntity.getName());
            wrapper.eq(studyCourseClassifyEntity.getStatus() != null,"status",studyCourseClassifyEntity.getStatus());
        }
        wrapper.orderByDesc("sort");
        wrapper.orderByDesc("create_time");
        return wrapper;
    }


    @Override
    public IPage<StudyCourseClassifyDto> pageStudyCourseClassify(Page<StudyCourseClassifyDto> page, StudyCourseClassifyDto studyCourseClassifyDto) {
        QueryWrapper<StudyCourseClassifyEntity> wrapper = getWrapper(studyCourseClassifyDto);
        Page<StudyCourseClassifyEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<StudyCourseClassifyEntity> studyCourseClassifyPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<StudyCourseClassifyDto> pageDto = new Page(studyCourseClassifyPage.getCurrent(), studyCourseClassifyPage.getSize(),studyCourseClassifyPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(studyCourseClassifyPage.getRecords(),StudyCourseClassifyDto.class));
        return pageDto;
    }

    @Override
    public IPage<StudyCourseClassifyDto> pageStudyCourseClassifyExtends(Page<StudyCourseClassifyDto> page, StudyCourseClassifyDto studyCourseClassifyDto){
        Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
        StudyCourseClassifyEntity studyCourseClassifyEntity = ConvertUtil.transformObj(studyCourseClassifyDto,StudyCourseClassifyEntity.class);
        List<StudyCourseClassifyEntity> studyCourseClassifyList=baseMapper.extendsList(pageMap,studyCourseClassifyEntity);
        Long total = baseMapper.extendsCount(studyCourseClassifyEntity);
        Page<StudyCourseClassifyDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
        pageDto.setRecords(ConvertUtil.transformObjList(studyCourseClassifyList,StudyCourseClassifyDto.class));
        return pageDto;
    }
    @Override
    public StudyCourseClassifyDto getByIdExtends(Long id){
        StudyCourseClassifyEntity  studyCourseClassifyEntity = baseMapper.getByIdExtends(id);
        return ConvertUtil.transformObj(studyCourseClassifyEntity,StudyCourseClassifyDto.class);
    }

    @Override
    public List<StudyCourseClassifyDto> listStudyCourseClassify(StudyCourseClassifyDto studyCourseClassifyDto){
        QueryWrapper<StudyCourseClassifyEntity> wrapper = getWrapper(studyCourseClassifyDto);
        List<StudyCourseClassifyEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,StudyCourseClassifyDto.class);
    }

    @Override
    public boolean checkName(StudyCourseClassifyEntity studyCourseClassifyEntity) {
        QueryWrapper<StudyCourseClassifyEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("name",studyCourseClassifyEntity.getName());
        wrapper.eq(studyCourseClassifyEntity.getParentId()!=null,"parent_id",studyCourseClassifyEntity.getParentId());
        wrapper.isNull(studyCourseClassifyEntity.getParentId()==null,"parent_id");
        wrapper.ne(studyCourseClassifyEntity.getId()!=null,"id",studyCourseClassifyEntity.getId());
        List<StudyCourseClassifyEntity> list = baseMapper.selectList(wrapper);
        return (list!=null&&list.size()>0)?true:false;
    }
}

